<?php

require_once 'database/DBConnection.php';
require_once 'ShopBasket.php';

class ShopBasketHandler {
		
	private $DBConnection = NULL;
	//Skulle egentligen kanske heta order och orderlines...
	private $m_userTable = "user";
	private $m_productTableName = "shopbasket";
	
		public function __construct(DBConnection $connection){
		$this->DBConnection = $connection;
	}
		
// Hämtar allt från tabellen shopbasket när specifik användare är inloggad
//-----------------------------------------GET---------------------------------------------------			
public function GetOrderlines($shopuserId) {
		$ret = array();
        
		//hämtar det som användaren har i sin shopbasket som INTE har status 1 d.v.s. 0 (status ett har produkterna då de slutligen är bekräftade 
		$sql = "SELECT * FROM $this->m_productTableName WHERE shopuser_pk = ? AND status = 0";
			    
        $stmt = $this->DBConnection->Prepare($sql);
		
		//nytt
		$stmt->bind_param("i", $shopuserId);
		
		$stmt->execute();
		
		//Nytt, istället för get_result
		$ret = $this->get($stmt);
		
		$stmt->close();
                
        return $ret; 
	}



// Hämtar allt från tabellen shopbasket när specifik användare är inloggad
//-----------------------------------------GET---------------------------------------------------			
public function GetOrderlinesBought($shopuserId) {
		$ret = array();
        
		$sql = "SELECT * FROM $this->m_productTableName WHERE shopuser_pk = ? AND status = 1";
			    
        $stmt = $this->DBConnection->Prepare($sql);
		
		//nytt
		$stmt->bind_param("i", $shopuserId);
		
		$stmt->execute();
		
		//Nytt, istället för get_result
		$ret = $this->get($stmt);
		
		$stmt->close();
                
        return $ret; 
	}




//----------------------------------------------INSERT-----------------------
public function InsertShopProduct ($shopuser, $shopprice, $product_pk) {

		$sql = "INSERT INTO `$this->m_productTableName`(`shopuser_pk`, `m_shopprice`, `product_pk`) VALUES (?,?,?)";
	
		$stmt = $this->DBConnection->Prepare($sql);
		
		$stmt->bind_param("iii", $shopuser, $shopprice, $product_pk);
		
	  	if ($stmt === FALSE) {
	    	return false;
	    }
	
		if ($stmt -> execute()) {
			$stmt -> close();
			return TRUE;
		}else{
			$stmt->close();
			return FALSE;
		}
	
	}

	//--------------------------------------UPDATE-------------------------
	public function UpdateShopProduct ($status) {
					
		$sql = "UPDATE $this->m_productTableName SET status = 1 WHERE shopuser_pk = ?";
	
		$stmt = $this->DBConnection->Prepare($sql);
		
		$stmt->bind_param("i", $status);
		
	  	if ($stmt === FALSE) {
	    	return false;
	    }
	
		if ($stmt -> execute()) {
			$stmt -> close();
			return TRUE;
		}else{
			$stmt->close();
			return FALSE;
		}
	}
	

	// ---------------------------------------DELETE -------------------------
	public function DeleteShopProduct($shopuser_pk, $product_pk) {
			
		//Limit 1 för att inte radera alla produkter som är likadana
		$sql = "DELETE FROM $this->m_productTableName WHERE shopuser_pk = ? AND product_pk = ? LIMIT 1";
	
		$stmt = $this->DBConnection->Prepare($sql);
		
		$stmt->bind_param("ii", $shopuser_pk, $product_pk);
		
		$stmt -> execute();
	
		$stmt -> close(); 

	return TRUE;
	}

	public function get($stmt){
		$ret = array();
		
		$stmt->bind_result($shopbasket_pk, $shopuser_pk, $m_shopprice, $product_pk, $status);
		
		while ($stmt->fetch()) {
			$shopbasket = new ShopBasket();
			$shopbasket->shopbasket_pk = $shopbasket_pk;
			$shopbasket->shopuser_pk =$shopuser_pk;
			$shopbasket->m_shopprice=$m_shopprice;
			$shopbasket->product_pk=$product_pk;
			$shopbasket->status=$status;

        	$ret[] = $shopbasket;
    	}
		
		return $ret;
	}
}


